//
//  LZSearchCityViewController.m
//  laziz_Consumer
//
//  Created by xbnzd on 17/4/25.
//  Copyright © 2017年 XBN. All rights reserved.
//

#import "LZSearchCityViewController.h"
#import "UIViewController+NavigationItemSetting.h"
#import "UIView+Rotation.h"
#import "UIButton+EnlargeEdge.h"
#import "LZSearchCityCell.h"
#import "LZSearchCityHeaderView.h"
#import "CityManager.h"
#import "ProvinceModel.h"
#import "CitysModel.h"
#import "Masonry.h"
#import "extobjc.h"

static NSString *searchCell = @"cell";

@interface LZSearchCityViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray     *dataArr;
@end

@implementation LZSearchCityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.umLogPageViewName = @"城市列表页面";
    
    [self initUI];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

#pragma mark -
#pragma mark tableview delegate datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 52;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    static NSString *headerViewId = @"cityHeaderViewIdentifier";
    LZSearchCityHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headerViewId];
    if (!headerView) {
        headerView = [[LZSearchCityHeaderView alloc] initWithReuseIdentifier:headerViewId];
    }
    
    if (self.dataArr.count>section) {
        ProvinceModel *m = self.dataArr[section];
        [headerView setLabelText:m.name];
    }
    
    return headerView;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    ProvinceModel *m = self.dataArr[section];
    return m.citysArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  
    if (section < self.dataArr.count-1) {
        return 10.0;
    }
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 52;
}


- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    static NSString *footerViewId = @"cityFooterViewIdentifier";
    UITableViewHeaderFooterView *footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:footerViewId];
    if (!footerView) {
        footerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:footerViewId];
        
        UIView *grayView = [[UIView alloc] init];
        grayView.backgroundColor = UIColorFromRGB(0xeeeeee, 1);
        [footerView addSubview:grayView];
        
        [grayView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(footerView);
        }];
    }
    
    return footerView;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    LZSearchCityCell *cell = [tableView dequeueReusableCellWithIdentifier:searchCell];
    if (!cell) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"LZSearchCityCell" owner:nil options:nil].firstObject;
        
        //        cell.backgroundColor = [UIColor clearColor];
        
    }
    CitysModel *cityM;
 
    ProvinceModel *m = self.dataArr[indexPath.section];
    NSArray *citysArr = m.citysArr;
    cityM = citysArr[indexPath.row];
    cell.lineView.hidden = (indexPath.row == citysArr.count-1);
    
    cell.contentLabel.text = cityM.name;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    CitysModel *cityM;

    ProvinceModel *m = self.dataArr[indexPath.section];
    NSArray *citysArr = m.citysArr;
    cityM = citysArr[indexPath.row];
    
    if (self.selectCityBlock) {
        self.selectCityBlock(cityM.name,cityM.cityNO);
    }
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark -
#pragma mark UI

- (void)initUI
{
    self.title = LZLocalizedString(@"res_auth_select_city_title", nil);
    
    _dataArr = [CityManager returnDataArrForTableView];
    
    [self.view addSubview:self.tableView];
  
    [self setSubviewsAutolayout];
}


- (void)setSubviewsAutolayout
{
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(0);
        make.trailing.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
}


#pragma mark -
#pragma mark 初始化

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.sectionFooterHeight = 0;
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    
    return _tableView;
}

@end
